package com.gy.boot.common.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class IEntity extends LinkedHashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = -730848974094928465L;

    public IEntity() {}

    public IEntity(int size) { super((int) (size / 0.75f) + 1); }

    public static IEntity create(int size) { return new IEntity(size); }

    public static IEntity create(Map<String, Object> params) {
        return params == null ? new IEntity() : new IEntity(params.size()).setAll(params);
    }

    // 相当于 put
    public IEntity set(String k, Object v) {
        this.put(k, v);
        return this;
    }

    // 相当于 putAll
    public IEntity setAll(Map<String, Object> map) {
        if (map == null) { return this; }
        this.putAll(map);
        return this;
    }

    /**
     * 过滤Map保留指定键值对，如果键不存在跳过
     * */
    public IEntity filter(String... keys) {
        final IEntity result = new IEntity();
        for (String key : keys) {
            if(this.containsKey(key)) {
                result.put(key, this.get(key));
            }
        }
        return result;
    }

    public String getStr(String k) {
        final Object obj = this.get(k);
        if (obj == null) { return null; }
        return String.valueOf(obj);
    }

    public boolean getBo(String k) {
        Boolean bool = getBool(k);
        return bool != null && bool;
    }

    public Boolean getBool(String k) {
        final Object v = this.get(k);
        if (v == null) { return null; }
        if (v instanceof Boolean) {
            return (Boolean) v;
        }
        String str = String.valueOf(v);
        return "1".equals(str);
    }

    public Integer getInt(String k) {
        final Object obj = this.get(k);
        if (obj == null) { return null; }
        try {
            return Integer.parseInt(obj.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public Long getLong(String k) {
        final Object obj = this.get(k);
        if (obj == null) { return null; }
        try {
            return Long.parseLong(obj.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public Double getDouble(String k) {
        final Object obj = this.get(k);
        if (obj == null) { return null; }
        try {
            return Double.parseDouble(obj.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public BigDecimal getBigDecimal(String k) {
        final String v = this.getStr(k);
        if (v == null) { return null; }
        try {
            return new BigDecimal(v);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 获取时间
     * */
    public DateTime getDataTime(String k) {
        final Object obj = this.get(k);
        if (obj == null) {
            return null;
        }
        if (obj instanceof Date) {
            return DateTime.of((Date) obj);
        }
        if (obj instanceof Long) {
            return DateTime.of((Long) obj);
        }
        if (obj instanceof String) {
            return DateUtil.parse((String) obj);
        }
        return null;
    }

    public IEntity getIEntity(String key) {
        final Object obj = this.get(key);
        if (obj == null) {
            return null;
        }
        if (obj instanceof IEntity) {
            return (IEntity) obj;
        }
        if (obj instanceof Map) {
            return create((Map) obj);
        }
        return null;
    }

    /**
     * 转有序JSON字符串
     * */
    public String toJsonStr() {
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.putAll(this);
        return jsonObject.toString();
    }

}
